package com.falm.picturebackend.exception;

import java.text.MessageFormat;

/**
 * 异常处理工具类
 */
public class ThrowUtils {

    public static void throwIf(boolean condition, RuntimeException runtimeException) {
        if (condition) {
            throw runtimeException;
        }
    }

    /**
     * 自定义异常处理
     * @param condition 判断条件
     * @param errorCode 返回码
     */
    public static void throwIf(boolean condition, ErrorCode errorCode) {
        throwIf(condition, new BusinessException(errorCode));
    }

    public static void throwIf(boolean condition, ErrorCode errorCode, String message) {
        throwIf(condition, new BusinessException(errorCode, message));
    }
    
    public static void throwIf(boolean condition, ErrorCode errorCode, String message, Object... params) {
        if (condition) {
            String formattedMessage = MessageFormat.format(message, params);
            throwIf(true, new BusinessException(errorCode, formattedMessage));
        }
    }
}